home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-4.0 / bfd / opncls.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-24  |  9.3 KB  |  386 lines

  1. /* opncls.c -- open and close a BFD.
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* $Id: opncls.c,v 1.24 1991/08/21 21:55:01 steve Exp $ */
  22.  
  23. #include <sysdep.h>
  24. #include "bfd.h"
  25. #include "libbfd.h"
  26. #include "obstack.h"
  27. extern void bfd_cache_init();
  28. FILE *bfd_open_file();
  29.  
  30. /* fdopen is a loser -- we should use stdio exclusively.  Unfortunately
  31.    if we do that we can't use fcntl.  */
  32.  
  33.  
  34. #define obstack_chunk_alloc malloc
  35. #define obstack_chunk_free free
  36.  
  37. /* Return a new BFD.  All BFD's are allocated through this routine.  */
  38.  
  39. bfd *new_bfd()
  40. {
  41.   bfd *nbfd;
  42.  
  43.   nbfd = (bfd *)zalloc (sizeof (bfd));
  44.   if (!nbfd)
  45.     return 0;
  46.  
  47.   obstack_begin((PTR)&nbfd->memory, 128);
  48.   
  49.   nbfd->direction = no_direction;
  50.   nbfd->iostream = NULL;
  51.   nbfd->where = 0;
  52.   nbfd->sections = (asection *)NULL;
  53.   nbfd->format = bfd_unknown;
  54.   nbfd->my_archive = (bfd *)NULL;
  55.   nbfd->origin = 0;                   
  56.   nbfd->opened_once = false;
  57.   nbfd->output_has_begun = false;
  58.   nbfd->section_count = 0;
  59.   nbfd->usrdata = (PTR)NULL;
  60.   nbfd->sections = (asection *)NULL;
  61.   nbfd->cacheable = false;
  62.   nbfd->flags = NO_FLAGS;
  63.   nbfd->mtime_set = 0;
  64.   return nbfd;
  65. }
  66.  
  67. /* Allocate a new BFD as a member of archive OBFD.  */
  68.  
  69. bfd *new_bfd_contained_in(obfd)
  70. bfd *obfd;
  71. {
  72.     bfd *nbfd = new_bfd();
  73.     nbfd->xvec = obfd->xvec;
  74.     nbfd->my_archive = obfd;
  75.     nbfd->direction = read_direction;
  76.     return nbfd;
  77. }
  78.  
  79. /*doc*
  80. @section Opening and Closing BFDs
  81.  
  82. */
  83. /*proto*
  84. *i bfd_openr
  85. Opens the file supplied (using @code{fopen}) with the target supplied, it
  86. returns a pointer to the created BFD.
  87.  
  88. If NULL is returned then an error has occured.
  89. Possible errors are no_memory, invalid_target or system_call error.
  90. *; PROTO(bfd*, bfd_openr, (CONST char *filename,CONST char*target));
  91. *-*/
  92.  
  93. bfd *
  94. DEFUN(bfd_openr, (filename, target),
  95.       CONST char *filename AND
  96.       CONST char *target)
  97. {
  98.   bfd *nbfd;
  99.   bfd_target *target_vec;
  100.  
  101.   nbfd = new_bfd();
  102.   if (nbfd == NULL) {
  103.     bfd_error = no_memory;
  104.     return NULL;
  105.   }
  106.  
  107.   target_vec = bfd_find_target (target, nbfd);
  108.   if (target_vec == NULL) {
  109.     bfd_error = invalid_target;
  110.     return NULL;
  111.   }
  112.  
  113.   nbfd->filename = filename;
  114.   nbfd->direction = read_direction; 
  115.  
  116.   if (bfd_open_file (nbfd) == NULL) {
  117.     bfd_error = system_call_error;    /* File didn't exist, or some such */
  118.     bfd_release(nbfd,0);
  119.     return NULL;
  120.   }
  121.   return nbfd;
  122. }
  123.  
  124.  
  125. /* Don't try to `optimize' this function:
  126.  
  127.    o - We lock using stack space so that interrupting the locking
  128.        won't cause a storage leak.
  129.    o - We open the file stream last, since we don't want to have to
  130.        close it if anything goes wrong.  Closing the stream means closing
  131.        the file descriptor too, even though we didn't open it.
  132.  */
  133. /*proto*
  134. *i bfd_fdopenr
  135. bfd_fdopenr is to bfd_fopenr much like  fdopen is to fopen. It opens a BFD on
  136. a file already described by the @var{fd} supplied. 
  137.  
  138. Possible errors are no_memory, invalid_target and system_call error.
  139. *;  PROTO(bfd *, bfd_fdopenr,
  140.     (CONST char *filename, CONST char *target, int fd));
  141. *-*/
  142.  
  143. bfd *
  144. DEFUN(bfd_fdopenr,(filename, target, fd),
  145.       CONST char *filename AND
  146.       CONST char *target AND
  147.       int fd)
  148. {
  149.   bfd *nbfd;
  150.   bfd_target *target_vec;
  151.   int fdflags;
  152.  
  153.   bfd_error = system_call_error;
  154.   
  155.   fdflags = fcntl (fd, F_GETFL, NULL);
  156.   if (fdflags == -1) return NULL;
  157.  
  158.  
  159.   nbfd = new_bfd();
  160.  
  161.   if (nbfd == NULL) {
  162.     bfd_error = no_memory;
  163.     return NULL;
  164.   }
  165.  
  166.   target_vec = bfd_find_target (target, nbfd);
  167.   if (target_vec == NULL) {
  168.     bfd_error = invalid_target;
  169.     return NULL;
  170.   }
  171.  
  172.   /* if the fd were open for read only, this still would not hurt: */
  173.   nbfd->iostream = (char *) fdopen (fd, "r+"); 
  174.   if (nbfd->iostream == NULL) {
  175.     (void) obstack_free (&nbfd->memory, (PTR)0);
  176.     return NULL;
  177.   }
  178.   
  179.   /* OK, put everything where it belongs */
  180.  
  181.   nbfd->filename = filename;
  182.  
  183.   /* As a special case we allow a FD open for read/write to
  184.      be written through, although doing so requires that we end
  185.      the previous clause with a preposition.  */
  186.   switch (fdflags & O_ACCMODE) {
  187.   case O_RDONLY: nbfd->direction = read_direction; break;
  188.   case O_WRONLY: nbfd->direction = write_direction; break;  
  189.   case O_RDWR: nbfd->direction = both_direction; break;
  190.   default: abort ();
  191.   }
  192.                    
  193.  
  194.   bfd_cache_init (nbfd);
  195.  
  196.   return nbfd;
  197. }
  198.  
  199. /** bfd_openw -- open for writing.
  200.   Returns a pointer to a freshly-allocated BFD on success, or NULL.
  201.  
  202.   See comment by bfd_fdopenr before you try to modify this function. */
  203.  
  204. /*proto* bfd_openw
  205. Creates a BFD, associated with file @var{filename}, using the file
  206. format @var{target}, and returns a pointer to it.
  207.  
  208. Possible errors are system_call_error, no_memory, invalid_target.
  209. *; PROTO(bfd *, bfd_openw, (CONST char *filename, CONST char *target));
  210. */
  211.  
  212. bfd *
  213. DEFUN(bfd_openw,(filename, target),
  214.       CONST char *filename AND
  215.       CONST char *target)
  216. {
  217.   bfd *nbfd;
  218.   bfd_target *target_vec;
  219.   
  220.   bfd_error = system_call_error;
  221.  
  222.   /* nbfd has to point to head of malloc'ed block so that bfd_close may
  223.      reclaim it correctly. */
  224.  
  225.   nbfd = new_bfd();
  226.   if (nbfd == NULL) {
  227.     bfd_error = no_memory;
  228.     return NULL;
  229.   }
  230.  
  231.   target_vec = bfd_find_target (target, nbfd);
  232.   if (target_vec == NULL) return NULL;
  233.  
  234.   nbfd->filename = filename;
  235.   nbfd->direction = write_direction;
  236.  
  237.   if (bfd_open_file (nbfd) == NULL) {
  238.     bfd_error = system_call_error;    /* File not writeable, etc */
  239.     (void) obstack_free (&nbfd->memory, (PTR)0);
  240.     return NULL;
  241.   }
  242.   return nbfd;
  243. }
  244.  
  245. /*proto* bfd_close
  246. This function closes a BFD. If the BFD was open for writing, then
  247. pending operations are completed and the file written out and closed.
  248. If the created file is executable, then @code{chmod} is called to mark
  249. it as such.
  250.  
  251. All memory attached to the BFD's obstacks is released. 
  252.  
  253. @code{true} is returned if all is ok, otherwise @code{false}.
  254. *; PROTO(boolean, bfd_close,(bfd *));
  255. */
  256.  
  257. boolean
  258. DEFUN(bfd_close,(abfd),
  259.       bfd *abfd)
  260. {
  261.   if (!bfd_read_p(abfd))
  262.     if (BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)) != true)
  263.       return false;
  264.  
  265.   if (BFD_SEND (abfd, _close_and_cleanup, (abfd)) != true) return false;
  266.  
  267.   bfd_cache_close(abfd);
  268.  
  269.   /* If the file was open for writing and is now executable,
  270.      make it so */
  271.   if (abfd->direction == write_direction 
  272.       && abfd->flags & EXEC_P) {
  273.     struct stat buf;
  274.     stat(abfd->filename, &buf);
  275. #ifndef S_IXUSR
  276. #define S_IXUSR 0100    /* Execute by owner.  */
  277. #endif
  278. #ifndef S_IXGRP
  279. #define S_IXGRP 0010    /* Execute by group.  */
  280. #endif
  281. #ifndef S_IXOTH
  282. #define S_IXOTH 0001    /* Execute by others.  */
  283. #endif
  284.  
  285.     chmod(abfd->filename,buf.st_mode | S_IXUSR | S_IXGRP | S_IXOTH);
  286.   }
  287.   (void) obstack_free (&abfd->memory, (PTR)0);
  288.   /* FIXME, shouldn't we de-allocate the bfd as well? */
  289.   return true;
  290. }
  291.  
  292. /*proto* bfd_create
  293. This routine creates a new BFD in the manner of @code{bfd_openw}, but without
  294. opening a file. The new BFD takes the target from the target used by
  295. @var{template}. The format is always set to @code{bfd_object}.
  296.  
  297. *; PROTO(bfd *, bfd_create, (CONST char *filename, bfd *template));
  298. */
  299.  
  300. bfd *
  301. DEFUN(bfd_create,(filename, template),
  302.       CONST char *filename AND
  303.       bfd *template)
  304. {
  305.   bfd *nbfd = new_bfd();
  306.   if (nbfd == (bfd *)NULL) {
  307.     bfd_error = no_memory;
  308.     return (bfd *)NULL;
  309.   }
  310.   nbfd->filename = filename;
  311.   if(template) {
  312.     nbfd->xvec = template->xvec;
  313.   }
  314.   nbfd->direction = no_direction;
  315.   bfd_set_format(nbfd, bfd_object);
  316.   return nbfd;
  317. }
  318.  
  319. /* Memory allocation */
  320.  
  321. DEFUN(PTR bfd_alloc_by_size_t,(abfd, size),
  322.       bfd *abfd AND
  323.       size_t size)
  324. {
  325.   PTR res = obstack_alloc(&(abfd->memory), size);
  326.   return res;
  327. }
  328.  
  329. DEFUN(void bfd_alloc_grow,(abfd, ptr, size),
  330.       bfd *abfd AND
  331.       PTR ptr AND
  332.       bfd_size_type size)
  333. {
  334.   (void)   obstack_grow(&(abfd->memory), ptr, size);
  335. }
  336. DEFUN(PTR bfd_alloc_finish,(abfd),
  337.       bfd *abfd)
  338. {
  339.   return obstack_finish(&(abfd->memory));
  340. }
  341.  
  342. DEFUN(PTR bfd_alloc, (abfd, size),
  343.       bfd *abfd AND
  344.       bfd_size_type size)
  345. {
  346.   return bfd_alloc_by_size_t(abfd, (size_t)size);
  347. }
  348.  
  349. DEFUN(PTR bfd_zalloc,(abfd, size),
  350.       bfd *abfd AND
  351.       bfd_size_type size)
  352. {
  353.   PTR res = bfd_alloc(abfd, size);
  354.   memset(res, 0, (size_t)size);
  355.   return res;
  356. }
  357.  
  358. DEFUN(PTR bfd_realloc,(abfd, old, size),
  359.       bfd *abfd AND
  360.       PTR old AND
  361.       bfd_size_type size)
  362. {
  363.   PTR res = bfd_alloc(abfd, size);
  364.   memcpy(res, old, (size_t)size);
  365.   return res;
  366. }
  367.  
  368. /*proto* bfd_alloc_size
  369. Return the number of bytes in the obstacks connected to the supplied
  370. BFD.
  371. *; PROTO(bfd_size_type,bfd_alloc_size,(bfd *abfd));
  372. */
  373.  
  374. bfd_size_type
  375. DEFUN( bfd_alloc_size,(abfd),
  376.       bfd *abfd)
  377. {
  378.   struct _obstack_chunk *chunk = abfd->memory.chunk;
  379.   size_t size = 0;
  380.   while (chunk) {
  381.     size += chunk->limit - &(chunk->contents[0]);
  382.     chunk = chunk->prev;
  383.   }
  384.   return size;
  385. }
  386.